<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['appraise:appraise:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['appraise:appraise:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['appraise:appraise:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="appraiseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="估价ID" align="center" prop="appraiseId" />
      <el-table-column label="小区ID" align="center" prop="plotId" />
      <el-table-column label="房屋面积" align="center" prop="addr" />
      <el-table-column label="卧室数量" align="center" prop="bedroom" />
      <el-table-column label="客厅数量" align="center" prop="drawing" />
      <el-table-column label="卫生间数量" align="center" prop="restroom" />
      <el-table-column label="楼层" align="center" prop="floor" />
      <el-table-column label="楼层数量" align="center" prop="floorNum" />
      <el-table-column label="朝向Id" align="center" prop="orientationId">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.orientation"
            :value="scope.row.orientationId"
          />
        </template>
      </el-table-column>
      <el-table-column label="赠送面积" align="center" prop="freeAddr" />
      <el-table-column label="装修费用" align="center" prop="fitmentPrice" />
      <el-table-column
        label="装修完成时间"
        align="center"
        prop="fitmentOverTime"
      />

      <el-table-column label="通风情况" align="center" prop="ventilate">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wind" :value="scope.row.ventilate" />
        </template>
      </el-table-column>
      <el-table-column label="采光情况" align="center" prop="light" />
      <el-table-column label="楼栋位置" align="center" prop="streeAddr" />
      <el-table-column label="景观" align="center" prop="scenery" />
      <el-table-column label="噪音影响" align="center" prop="noise" />
      <el-table-column label=" 厌恶设施" align="center" prop="hateBuilder" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="模拟价格" align="center" prop="monyPrice" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['appraise:appraise:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['appraise:appraise:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改买房估价对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="小区ID" prop="plotId">
          <el-select v-model="form.plotId" placeholder="请选择">
            <el-option
              v-for="item in plotList"
              :key="item.plotId"
              :label="item.plotName"
              :value="item.plotId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房屋面积" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入房屋面积" />
        </el-form-item>
        <el-form-item label="卧室数量" prop="bedroom">
          <el-input v-model="form.bedroom" placeholder="请输入卧室数量" />
        </el-form-item>
        <el-form-item label="客厅数量" prop="drawing">
          <el-input v-model="form.drawing" placeholder="请输入客厅数量" />
        </el-form-item>
        <el-form-item label="卫生间数量" prop="restroom">
          <el-input v-model="form.restroom" placeholder="请输入卫生间数量" />
        </el-form-item>
        <el-form-item label="楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入楼层" />
        </el-form-item>
        <el-form-item label="楼层数量" prop="floorNum">
          <el-input v-model="form.floorNum" placeholder="请输入楼层数量" />
        </el-form-item>
        <el-form-item label="朝向id" prop="orientationId">
          <el-select v-model="form.orientationId" placeholder="请选择">
            <el-option
              v-for="item in orientation"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="赠送面积" prop="freeAddr">
          <el-input v-model="form.freeAddr" placeholder="请输入赠送面积" />
        </el-form-item>
        <el-form-item label="装修费用" prop="fitmentPrice">
          <el-input v-model="form.fitmentPrice" placeholder="请输入装修费用" />
        </el-form-item>
        <el-form-item label="通风情况" prop="ventilate">
          <el-select v-model="form.ventilate" placeholder="请选择">
            <el-option
              v-for="item in ventilate"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="采光情况" prop="light">
          <el-select v-model="form.light" placeholder="请选择">
            <el-option
              v-for="item in light"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼栋位置" prop="streeAddr">
          <el-input v-model="form.streeAddr" placeholder="请输入楼栋位置" />
        </el-form-item>
        <el-form-item label="景观" prop="scenery">
          <el-input v-model="form.scenery" placeholder="请输入景观" />
        </el-form-item>
        <el-form-item label="噪音影响" prop="noise">
          <el-input v-model="form.noise" placeholder="请输入噪音影响" />
        </el-form-item>
        <el-form-item label=" 厌恶设施" prop="hateBuilder">
          <el-input v-model="form.hateBuilder" placeholder="请输入 厌恶设施" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="模拟价格" prop="monyPrice">
          <el-input v-model="form.monyPrice" placeholder="请输入模拟价格" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAppraise,
  getAppraise,
  delAppraise,
  addAppraise,
  updateAppraise,
} from "@/api/appraise/appraise";
import { getCity, getPlot } from "@/api/house/myhouse";
export default {
  name: "Appraise",
  dicts: ["is_putaway", "is_examine", "orientation", "wind"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 买房估价表格数据
      appraiseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //省
      city1: [],
      city2: [],
      city3: [],

      //上架状态
      putway: [
        {
          value: "1",
          label: "已上架",
        },
        {
          value: "0",
          label: "未上架",
        },
      ],
      //通风情况
      ventilate: [
        {
          value: 1,
          label: "南北通风",
        },
        {
          value: 0,
          label: "东西通风",
        },
      ],
      //采光情况
      light: [
        {
          value: 0,
          label: "强",
        },
        {
          value: 1,
          label: "一般",
        },
        {
          value: 2,
          label: "弱",
        },
      ],
      //审核状态
      examine: [
        {
          value: "1",
          label: "审核通过",
        },
        {
          value: "0",
          label: "审核不通过",
        },
      ],
      //朝向状态
      orientation: [
        {
          value: 0,
          label: "朝南",
        },
        {
          value: 1,
          label: "朝北",
        },
        {
          value: 2,
          label: "朝东",
        },

        {
          value: 3,
          label: "朝西",
        },
      ],

      //获取小区
      plotList: [],
    };
  },
  created() {
    this.getList();
    this.citys1();
    this.getPlots();
  },
  methods: {
    //获取小区
    getPlots() {
      getPlot().then((res) => {
        this.plotList = res;
      });
    },
    //获取省
    citys1() {
      getCity(0).then((res) => {
        this.city1 = res;
        // console.log(res);
      });
    },
    //获取市
    citys2(id) {
      this.city2 = [];
      getCity(id).then((res) => {
        this.city2 = res;
        // console.log("2222");

        // console.log(res);
      });
    },
    //获取区
    citys3(id) {
      this.city3 = [];
      getCity(id).then((res) => {
        this.city3 = res;
        // console.log("3333");

        // console.log(res);
      });
    },
    /** 查询买房估价列表 */
    getList() {
      this.loading = true;
      listAppraise(this.queryParams).then((response) => {
        this.appraiseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        appraiseId: null,
        plotId: null,
        addr: null,
        bedroom: null,
        drawing: null,
        restroom: null,
        floor: null,
        floorNum: null,
        orientationId: null,
        freeAddr: null,
        fitmentPrice: null,
        fitmentOverTime: null,
        ventilate: null,
        light: null,
        streeAddr: null,
        scenery: null,
        noise: null,
        hateBuilder: null,
        userId: null,
        monyPrice: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.appraiseId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加买房估价";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const appraiseId = row.appraiseId || this.ids;
      getAppraise(appraiseId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改买房估价";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.appraiseId != null) {
            updateAppraise(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppraise(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const appraiseIds = row.appraiseId || this.ids;
      this.$modal
        .confirm('是否确认删除买房估价编号为"' + appraiseIds + '"的数据项？')
        .then(function () {
          return delAppraise(appraiseIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "appraise/appraise/export",
        {
          ...this.queryParams,
        },
        `appraise_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
